package com.pickgliss.effect
{
	import com.pickgliss.ui.ComponentFactory;
	import com.pickgliss.utils.ClassUtils;
	import com.pickgliss.utils.DisplayUtils;
	import com.pickgliss.utils.ObjectUtils;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * 
	 * @author Herman
	 * 添加显示效果到目标显示对象
	 * 
	 */	
	public class AddMovieEffect extends BaseEffect implements IEffect
	{
		public function AddMovieEffect($id:int)
		{
			super($id);
		}
		
		private var _movies:Vector.<DisplayObject>;
		private var _rectangles:Vector.<Rectangle>;
		private var _data:Array;
		override public function init(target:DisplayObject, data:Array):void
		{
			super.init(target,data);
			_data = data;
			creatMovie();
		}
		
		public function get movie():Vector.<DisplayObject>
		{
			return _movies;
		}
		
		override public function dispose():void
		{
			super.dispose();
			stop();
			_movies = null;
		}
		
		override public function play():void
		{
			super.play();
			for(var i:int = 0;i<_movies.length;i++)
			{
				
				if(_movies[i] is MovieClip)
				{
					MovieClip(_movies[i]).play();
				}
				if(_target.parent)_target.parent.addChild(_movies[i]);
				_movies[i].x = _target.x;
				_movies[i].y = _target.y;
				if(_rectangles.length - 1 >= i)
				{
					_movies[i].x += _rectangles[i].x;
					_movies[i].y += _rectangles[i].y;
				}
			}
		}
		
		override public function stop():void
		{
			super.stop();
			for(var i:int = 0;i<_movies.length;i++)
			{
				if(_movies[i] is MovieClip)
				{
					MovieClip(_movies[i]).stop();
				}
				//修改没有上级容器报错
				if(_movies[i].parent)_movies[i].parent.removeChild(_movies[i]);
			}
		}
		
		public function creatMovie():void
		{
			_movies = new Vector.<DisplayObject>();
			_rectangles = new Vector.<Rectangle>();
			for(var i:int = 0;i<_data.length;i++)
			{
				if(_data[i] is DisplayObject)
				{
					_movies.push(_data[i]);
				}else if(_data[i] is String)
				{
					_movies.push(ComponentFactory.Instance.creat(_data[i]));
				}
			}
			
			for(var j:int = 0;j<_data.length;j++)
			{
				if(_data[j] is Point)
				{
					_rectangles.push(new Rectangle(_data[j].x,_data[j].y,0,0));
				}else if(_data[j] is Rectangle)
				{
					_rectangles.push(_data[j]);
				}
			}
			
			for(var k:int = 0;k<_movies.length;k++)
			{
				if(_movies[k] is InteractiveObject)
				{
					InteractiveObject(_movies[k]).mouseEnabled = false;
				}
				if(_movies[k] is DisplayObjectContainer)
				{
					DisplayObjectContainer(_movies[k]).mouseChildren = false;
					DisplayObjectContainer(_movies[k]).mouseEnabled = false;
				}
			}
		}
	}
}